package com.example.main;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;

public class DispatchKeyResolver {
    public OnKeyInputListener keyInputListener;


    private String barcode = "";

    public void ddd(Activity context, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            Log.e("123123", "dispatchKeyEvent: " + event.toString());
            char pressedKey = (char) event.getUnicodeChar();
            barcode += pressedKey;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
//            context.makeText(getApplicationContext(),
//                    "barcode---&gt;>>" + barcode, Toast.LENGTH_LONG)
//                    .show();
            Log.i("123123", barcode);
            //  Toast.makeText(MainActivity.this,barcode,Toast.LENGTH_LONG).show();
            barcode = "";
        }

    }


    public void resolveKeyEvent(KeyEvent event) {
        getInputChar(event);
    }

    public void getInputChar(KeyEvent event) {
        int keyCode = event.getKeyCode();
        char aChar;
        if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            //数字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        } else if (keyCode >= KeyEvent.KEYCODE_NUMPAD_0 && keyCode <= KeyEvent.KEYCODE_NUMPAD_9) {
            //数字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_NUMPAD_0);
        } else {
            //其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = '.';
                    break;
                case KeyEvent.KEYCODE_NUMPAD_DOT:
                    aChar = '.';
                    break;
                default:
                    return;
            }
        }
        keyInputListener.onKeyInput(aChar);
    }

    public void setKeyInputListener(OnKeyInputListener keyInputListener) {
        this.keyInputListener = keyInputListener;
    }

    public interface OnKeyInputListener {
        void onKeyInput(char aChar);
    }
}
